﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;


namespace ParkingLot.Entitys.DbSessionEntitys
{
    /// <summary>
    /// 数据库语句类
    /// </summary>
    [Serializable]
    public class DbSessionEntity
    {

        #region 字段

        private string m_CommandText = "";
        public string CommandText
        {
            get { return m_CommandText; }
            set { m_CommandText = value; }
        }

        /// <summary>
        /// key 条件名称  value 条件内容
        /// </summary>
        public XmlDictionary<string, ConditionInfo> DictConditionInfos = new XmlDictionary<string, ConditionInfo>();

        /// <summary>
        /// key 参数名称 value参数内容
        /// </summary>
        public XmlDictionary<string, ParameterInfo> DictParameterInfos = new XmlDictionary<string, ParameterInfo>();



        #endregion

        #region 构造函数

        public DbSessionEntity()
        {

        }

        public DbSessionEntity(string sql)
        {
            this.m_CommandText = sql;
        }
        #endregion

        #region 转换器
        /// <summary>
        /// string 转DbSessionEntity
        /// </summary>
        /// <param name="commandText"></param>
        public static implicit operator DbSessionEntity(string commandText)
        {
            return new DbSessionEntity(commandText);
        }

        /// <summary>
        /// StringBuilder转转DbSessionEntity
        /// </summary>
        /// <param name="commandText"></param>
        public static implicit operator DbSessionEntity(StringBuilder commandText)
        {
            return new DbSessionEntity(commandText.ToString());
        }
        #endregion

        public void AddParameter(string parameterName, object value)
        {
            if (!DictParameterInfos.Keys.Contains(parameterName))
            {
                DictParameterInfos.Add(parameterName, new ParameterInfo() { SqlValue = value });
            }
        }

        /// <summary>
        /// 填充参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        public void FillParameter(string parameterName, object value)
        {
            if (DictParameterInfos.Keys.Contains(parameterName))
            {
                DictParameterInfos[parameterName] = new ParameterInfo() { SqlValue = value };
            }
        }

        /// <summary>
        /// 添加条件查询
        /// </summary>
        /// <param name="conditionName">条件名称</param>
        /// <param name="conditionContent">条件内容</param>
        public void AddCondition(string conditionName, string conditionContent)
        {
            if (this.DictConditionInfos.ContainsKey(conditionName) == false)
            {
                this.DictConditionInfos.Add(conditionName, new ConditionInfo()
                {
                    ConditionContent = conditionContent
                });
            }
        }

        /// <summary>
        /// 设置条件
        /// </summary>
        /// <param name="conditionName">条件名称</param>
        /// <param name="parameterName">参数名称</param>
        /// <param name="value">参数值</param>
        public void SetCondition(string conditionName, string parameterName, object value)
        {
            if (this.DictConditionInfos.ContainsKey(conditionName) == false)
            {
                return;
            }

            this.DictConditionInfos[conditionName].IsUsed = true;
            DictParameterInfos.Add(parameterName, new ParameterInfo() { SqlValue = value });
        }

        /// <summary>
        /// 添加条件(条件伴随着参数)
        /// </summary>
        /// <param name="ConditionCommand">条件语句</param>
        /// <param name="parameterName">参数名称</param>
        /// <param name="value">参数值</param>
        public void AddCondition(string ConditionCommand, string parameterName, object value)
        {
            this.m_CommandText += ConditionCommand;
            DictParameterInfos.Add(parameterName, new ParameterInfo() { SqlValue = value });
        }

        

        public override string ToString()
        {
            string conditionValue = "";
            foreach (var key in DictConditionInfos.Keys)
            {
                if (DictConditionInfos[key].IsUsed)
                {
                    conditionValue += DictConditionInfos[key].ConditionContent;
                }
            }
            return this.CommandText + conditionValue;
        }
    }

    [Serializable]
    public class ParameterInfo
    {
        public object SqlValue = null;

        public ParameterInfo()
        {

        }

        public ParameterInfo(SqlParameter sqlParameter)
        {
            this.SqlValue = sqlParameter.SqlValue;
        }


        public static implicit operator ParameterInfo(SqlParameter sqlParameter)
        {
            return new ParameterInfo(sqlParameter);
        }
    }

    public class ConditionInfo
    {
        /// <summary>
        /// 条件内容
        /// </summary>
        public string ConditionContent;

        /// <summary>
        /// 是否使用
        /// </summary>
        public bool IsUsed = false;
    }


}
